﻿using System;
using Songhay;
using Songhay.DataAccess.Models;
using Songhay.Models;

namespace Songhay.DataAccess.Models.Extensions
{

    /// <summary>
    /// Extensions of <see cref="ISegment"/>
    /// </summary>
    public static class ISegmentExtensions
    {
        /// <summary>
        /// Sets the defaults.
        /// </summary>
        /// <param name="segment">The segment.</param>
        public static void SetDefaults(this ISegment segment)
        {
            if (segment == null) return;

            segment.CreateDate = DateTime.Now;
            segment.IsActive = true;
        }

        /// <summary>
        /// Converts the <see cref="ISegment" /> into a <see cref="DisplayItemModel"/>.
        /// </summary>
        /// <param name="segment">The ISegment.</param>
         public static DisplayItemModel ToDisplayItemModel(this ISegment segment)
         {
            return segment.ToDisplayItemModel(DisplayItemModelCategories.GenericWebSegment);
         }

        /// <summary>
         /// Converts the <see cref="ISegment" /> into a <see cref="DisplayItemModel"/>.
         /// </summary>
        /// <param name="segment">The ISegment.</param>
        /// <param name="category">The Category.</param>
         public static DisplayItemModel ToDisplayItemModel(this ISegment segment, string category)
         {
            if(segment == null) return null;

            var data = new DisplayItemModel();
            data.DisplayText = segment.SegmentName;
            data.Id = segment.SegmentId;
            data.ItemCategory = category;
            data.ItemName = segment.SegmentName;

            return data;
         }

         /// <summary>
         /// Converts the <see cref="ISegment"/> into a display text.
         /// </summary>
         /// <param name="data">The data.</param>
        public static string ToDisplayText(this ISegment data)
         {
             if (data == null) return null;

             var s = string.Format("SegmentId: {0}, SegmentName: {1}, IsActive: {2}, ParentSegmentId: {3}, CreateDate: {4}",
                 data.SegmentId, data.SegmentName, data.IsActive, data.ParentSegmentId, data.CreateDate);
             return s;
         }

         /// <summary>
         /// Converts the <see cref="ISegment"/> into a menu display item model.
         /// </summary>
         /// <param name="segment">The segment.</param>
         public static MenuDisplayItemModel ToMenuDisplayItemModel(this ISegment segment)
         {
             return segment.ToMenuDisplayItemModel(DisplayItemModelCategories.GenericWebSegment);
         }

         /// <summary>
         /// Converts the <see cref="ISegment"/> into a menu display item model.
         /// </summary>
         /// <param name="segment">The segment.</param>
         /// <param name="category">The category.</param>
         public static MenuDisplayItemModel ToMenuDisplayItemModel(this ISegment segment, string category)
         {
             if (segment == null) return null;

             var item = segment.ToDisplayItemModel(category);
             var menuItem = new MenuDisplayItemModel();
             FrameworkTypeUtility.SetProperties(item, menuItem);
             return menuItem;
         }

        /// <summary>
         /// Updates the specified segment.
         /// </summary>
         /// <param name="segment">The segment.</param>
         /// <param name="data">The data.</param>
         public static void Update(this ISegment segment, ISegment data)
         {
             if (segment == null) return;
             if (data == null) return;

             FrameworkTypeUtility.SetProperties(data, segment);
         }
    }
}
